/******************************************************************************
 *  _____     _____
 *  \  __\    \  __\
 *   \ \_\\    \ \_\\
 *    \  __\SS  \  __\roject
 *     \_\       \_\
 *
 * Mateusz Maciąg, Rafał Urbański
 * Komputerowe Systemy Sterowania
 *****************************************************************************/

#include "InputSquareDesigner.h"
#include "ui_InputSquareDesigner.h"

#include <QMessageBox>

using namespace std;

InputSquareDesigner::InputSquareDesigner(QWidget *parent) :
    InputDesigner(parent),
    ui(new Ui::InputSquareDesigner)
{
    ui->setupUi(this);
}

InputSquareDesigner::~InputSquareDesigner()
{
    delete ui;
}

void InputSquareDesigner::attachInput(RafMat::SimInterface::IConfigurableSPtr ctrl)
{
    m_inputConf = ctrl;

    try
    {
        string amplitude = m_inputConf->getParameter("amplitude");
        string period = m_inputConf->getParameter("period");
        string offset = m_inputConf->getParameter("offset");
        string phase = m_inputConf->getParameter("phase");
        string duty = m_inputConf->getParameter("duty");

        ui->cfgAmplitude->setValue(QString::fromStdString(amplitude).toDouble());
        ui->cfgPeriod->setValue(QString::fromStdString(period).toInt());
        ui->cfgOffset->setValue(QString::fromStdString(offset).toDouble());
        ui->cfgPhase->setValue(QString::fromStdString(phase).toDouble());
        ui->cfgDuty->setValue(QString::fromStdString(duty).toDouble());
    }
    catch(std::logic_error &e)
    {
        QMessageBox::warning(this, "Error", e.what());
    }
}

void InputSquareDesigner::applySettings()
{
    try
    {
        string amplitude = ui->cfgAmplitude->text().toStdString();
        string period = ui->cfgPeriod->text().toStdString();
        string offset = ui->cfgOffset->text().toStdString();
        string phase = ui->cfgPhase->text().toStdString();
        string duty = ui->cfgDuty->text().toStdString();

        m_inputConf->setParameter("amplitude", amplitude);
        m_inputConf->setParameter("period", period);
        m_inputConf->setParameter("offset", offset);
        m_inputConf->setParameter("phase", phase);
        m_inputConf->setParameter("duty", duty);
    }
    catch(std::logic_error &e)
    {
        QMessageBox::warning(this, "Error", e.what());
    }
}
